package com.bocloud.cmp.driver.providers.jd;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.bocloud.cmp.driver.providers.DatabaseDriver;
import com.bocloud.cmp.model.DbInstanceModel;
import com.bocloud.common.model.BsmResult;
import com.jcloud.cap.client.ClientConfig;
import com.jcloud.cap.client.IClient;
import com.jcloud.cap.client.JCloudClient;
import com.jcloud.cap.client.builder.v1.CreateDatabaseRequestBuilder;
import com.jcloud.cap.client.builder.v1.DeleteDatabaseRequestBuilder;
import com.jcloud.cap.client.builder.v1.QueryDatabaseDetailRequestBuilder;
import com.jcloud.cap.client.builder.v1.QueryDatabasesRequestBuilder;
import com.jcloud.cap.client.exception.ClientException;
import com.jcloud.cap.client.exception.ServerException;
import com.jcloud.cap.client.http.HttpConfig;
import com.jcloud.cap.data.model.v1.CreateDatabaseResponse;
import com.jcloud.cap.data.model.v1.DeleteDatabaseResponse;
import com.jcloud.cap.data.model.v1.QueryDatabaseDetailResponse;
import com.jcloud.cap.data.model.v1.QueryDatabasesResponse;

public class JdDatabaseDriver extends JdDriver implements DatabaseDriver {

	private static Logger logger = LoggerFactory.getLogger(JdDatabaseDriver.class);
	private IClient client;

	/**
	 * @param accessKey
	 * @param secretKey
	 * @param endpoint
	 */
	public JdDatabaseDriver(String accessKey, String secretKey, String endpoint, String region) {
		super(accessKey, secretKey, endpoint, region);
		ClientConfig config = new ClientConfig();
		HttpConfig httpConfig = new HttpConfig();
		// 调用地址
		httpConfig.setEndpoint(endpoint);
		httpConfig.setReadTimeout(20000);
		httpConfig.setConnectTimeout(20000);
		config.setHttpConfig(httpConfig);
		config.setCredential(this.getCredential().getCapCredential());
		client = new JCloudClient(config);
	}

	@Override
	public BsmResult listInstances() {
		QueryDatabasesRequestBuilder builder = QueryDatabasesRequestBuilder.create();
		this.init(builder, "instance", "queryDatabases", this.getRegion());
		try {
			QueryDatabasesResponse response = client.send(builder.build());
			return new BsmResult(true, response.getClusters().get(0), "success");
		} catch (ClientException | ServerException e) {
			logger.error("List instance [{}] exception!", e);
			return new BsmResult(false, "List instance failed");
		}
	}

	@Override
	public BsmResult createInstance(DbInstanceModel instance) {
		CreateDatabaseRequestBuilder builder = CreateDatabaseRequestBuilder.create();
		this.init(builder, "instance", "createDatabase", this.getRegion());
		try {
			CreateDatabaseResponse response = client
					.send(builder.clusterName(instance.getClusterName()).instanceName(instance.getName())
							.cpu(instance.getCpu()).memory(instance.getMemory()).disk(instance.getDisk())
							.userName(instance.getUsername()).password(instance.getPassword()).feeType(1).build());
			return new BsmResult(true, response.getRequestId(), "success");
		} catch (ClientException | ServerException e) {
			logger.error("Create instance [{}] exception!", e);
			return new BsmResult(false, "Create instance failed");
		}
	}

	@Override
	public BsmResult removeInstance(String instanceId) {
		DeleteDatabaseRequestBuilder builder = DeleteDatabaseRequestBuilder.create();
		this.init(builder, "instance", "deleteDatabase", this.getRegion());
		try {
			DeleteDatabaseResponse response = client.send(builder.clusterId(instanceId).build());
			return new BsmResult(true, response.getRequestId(), "success");
		} catch (ClientException | ServerException e) {
			logger.error("Reomve instance [{}] exception!", e);
			return new BsmResult(false, "Reomve instance failed");
		}
	}

	@Override
	public BsmResult instanceDetail(String instanceId) {
		QueryDatabaseDetailRequestBuilder builder = QueryDatabaseDetailRequestBuilder.create();
		this.init(builder, "instance", "queryDatabaseDetail", this.getRegion());
		try {
			QueryDatabaseDetailResponse response = client.send(builder.clusterId(instanceId).build());
			return new BsmResult(true, response.getCluster(), "success");
		} catch (ClientException | ServerException e) {
			logger.error("Query volume [{}] exception!", e);
			return new BsmResult(false, "Query volume failed");
		}
	}

	@Override
	public BsmResult modifyInstance(DbInstanceModel instance) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public BsmResult rebootInstance(String dbId) {
		// TODO Auto-generated method stub
		return null;
	}

}
